#!/usr/bin/env python
# encoding: utf-8
"""
@Author: Beam
@Mail:506556658@qq.com
@file: pika_recv.py
@time: 2017/5/17 14:38
"""

import pika,time
credentials = pika.PlainCredentials('admin', 'admin')   #设置rabbitmq连接账号密码，默认是guest，guest
connection = pika.BlockingConnection(pika.ConnectionParameters('192.168.6.248',5672,'/',credentials))
channel = connection.channel()   #建立连接通道
def callback(ch,method,properties,body):   #定义回调函数
    print(" [x] Received %r" % body)
    time.sleep(30)  #用于后面的测试no_ack
    print("method.delivery_tag",method.delivery_tag)  #第几条消息
    ch.basic_ack(delivery_tag=method.delivery_tag)

channel.basic_qos(prefetch_count=1)
channel.basic_consume(callback,  #当执行消费时，调用回调函数
                      queue='task_queue',
                      #no_ack=True ##默认是flase，True表示消费完以后不主动把状态通知rabbitmq，Flase表示如果消费者遇到{mq连接中断,服务挂掉,网络中断等}挂掉了，RabbitMQ会重新将该任务添加到队列中
                      )
print(' [-->] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()